const cartItems = [
  { id: 1, price: 100, quantity: 2, discount: 10 },
  { id: 2, price: 50, quantity: 5, discount: 0 },
  { id: 3, price: 200, quantity: 1, discount: 20 }
];

let total = 0;
let globalDiscount = 0.9;

for (var i in cartItems) {
  total += cartItems[i].price * cartItems[i].quantity * (1 - cartItems[i].discount / 100);
  if (total > 500) globalDiscount = 0.8;
}

total = total * globalDiscount;
console.log(total);